//
//  ZBNavigationController.m
//  ZBNavigation
//
//  Created by  on 11-11-22.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
//

#import "ZBNavigationController.h"
#import "ZBViewControllerProtocol.h"

@implementation ZBNavigationController

@synthesize zbNavigationBar=_zbNavigationBar;
@synthesize usingZBNavigationBar=_usingZBNavigationBar;

static void setViewcontrollerPushed(UIViewController *controller, BOOL pushed) {
    if ([controller conformsToProtocol:@protocol(ZBViewControllerProtocol)]) {
        id<ZBViewControllerProtocol> zbcontroller = (id<ZBViewControllerProtocol>)controller;
        zbcontroller.justPushed = pushed;
    } 
}

static void setViewcontrollerPoped(UIViewController *controller, BOOL poped) {
    if ([controller conformsToProtocol:@protocol(ZBViewControllerProtocol)]) {
        id<ZBViewControllerProtocol> zbcontroller = (id<ZBViewControllerProtocol>)controller;
        zbcontroller.justPoped = poped;
    } 
}

#pragma mark - View lifecycle

-(id)initWithRootViewController:(UIViewController *)rootViewController {
    self = [super initWithRootViewController:rootViewController];
    setViewcontrollerPushed(rootViewController,YES);
    setViewcontrollerPoped(rootViewController,NO);
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(void)setUsingZBNavigationBar:(BOOL)value {
    if (value!=_usingZBNavigationBar) {
        _usingZBNavigationBar=value;
    }
    if (_usingZBNavigationBar) {
        _zbNavigationBar = [[ZBNavigationBar alloc] initWithFrame:self.navigationBar.frame];
        _zbNavigationBar.naviController = self;
        [self.view addSubview:_zbNavigationBar];
    } else {
        [_zbNavigationBar removeFromSuperview];
        [_zbNavigationBar release];
        _zbNavigationBar = nil;
    }
}

-(void)setNavBarBackgroundImage:(UIImage *)navBarBackgroundImage {
    if ([self.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)]) {
        [self.navigationBar setBackgroundImage:navBarBackgroundImage forBarMetrics:UIBarMetricsDefault]; 
    }
}

- (void)dealloc
{
    [_zbNavigationBar release];
    [super dealloc];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    if (_usingZBNavigationBar&&!_zbNavigationBar.naviBarRendered) {
        [_zbNavigationBar changeTopView:self.topViewController animated:NO pushOrPop:YES];
    }
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (self.topViewController) {
        setViewcontrollerPushed(self.topViewController,NO);
    }
    setViewcontrollerPushed(viewController,YES);
    setViewcontrollerPoped(viewController,NO);
    [super pushViewController:viewController animated:animated];
    if (_usingZBNavigationBar) {
        [_zbNavigationBar changeTopView:self.topViewController animated:animated pushOrPop:YES];
    }
}

- (UIViewController *)popViewControllerAnimated:(BOOL)animated
{
    UIViewController *controller =  [super popViewControllerAnimated:animated]; 
    setViewcontrollerPoped(controller,YES);
    if (_usingZBNavigationBar) {
        [_zbNavigationBar changeTopView:self.topViewController animated:animated pushOrPop:NO];
    }
    return controller;
}

- (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    NSArray * array = [super popToViewController:viewController animated:animated]; 
    for (UIViewController *controller in array) {
        setViewcontrollerPoped(controller,YES);
    }
    if (_usingZBNavigationBar) {
        [_zbNavigationBar changeTopView:self.topViewController animated:animated pushOrPop:YES];
    }
    return array;
}

- (NSArray *)popToRootViewControllerAnimated:(BOOL)animated
{
    NSArray * array = [super popToRootViewControllerAnimated:animated]; 
    for (UIViewController *controller in array) {
        setViewcontrollerPoped(controller,YES);
    }
    if (_usingZBNavigationBar) {
        [_zbNavigationBar changeTopView:self.topViewController animated:animated pushOrPop:YES];
    }
    return array;
}
@end
